package com.mz.ozy.activity.shop;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.mz.ozy.R;
import com.mz.ozy.adapter.MallBannerAdapter;
import com.mz.ozy.adapter.MallCenterAdapter;
import com.mz.ozy.adapter.SaveDayAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.HomeBean;
import com.mz.ozy.bean.MallBean;
import com.mz.ozy.mvp.presenter.MallPresenter;
import com.mz.ozy.mvp.view.MallContract;
import com.mz.ozy.utils.AppUtils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MallActivity extends BaseActivity<MallContract.View, MallPresenter> implements MallContract.View {


    @BindView(R.id.bannerPager)
    RollPagerView rollPagerView;
    @BindView(R.id.iv_bannerPager)
    ImageView ivBannerPager;
    @BindView(R.id.iv_kt_topic)
    ImageView ivKtTopic;
    @BindView(R.id.iv_xf_topic)
    ImageView ivXfTopic;
    @BindView(R.id.tv_kt_title)
    TextView tvKtTitle;
    @BindView(R.id.tv_xf_title)
    TextView tvXfTitle;

    @BindView(R.id.rv_save_days)
    RecyclerView mRvSaveDays;
    @BindView(R.id.homeRecyclerView)
    RecyclerView homeRecyclerView;
    @BindView(R.id.homeRecyclerView2)
    RecyclerView homeRecyclerView2;
    RelativeLayout rlCar;
    @BindView(R.id.carNumberTv)
    TextView mCarNumberTv;//购物车数量
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;

    private SaveDayAdapter mSaveDayAdapter;
    private MallCenterAdapter mMallCenterAdapter;
    private MallCenterAdapter mMallCenterAdapter2;

    private boolean isFirst = true;
    private MallBannerAdapter mBannerAdapter;
    private List<HomeBean.BannerListBean> mBannerListBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall);
        ButterKnife.bind(this);
        titleBar = findViewById(R.id.titleBar);
        rlCar = findViewById(R.id.rl_car);
        setTitleBarMargin();

        rlCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MallActivity.this, CarActivity.class);
                startActivity(intent);
            }
        });
        initView();
    }

    /**
     * 设置沉浸式状态栏时，要把标题栏往下移动状态栏的高度
     */
    protected void setTitleBarMargin() {
        int length = AppUtils.getStatusBarHeight(this);
        //注意布局是哪种Layout
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(titleBar.getLayoutParams());
        lp.setMargins(0, length, 0, 0);
        titleBar.setLayoutParams(lp);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getMallInfo(this, isFirst);
        isFirst = false;
    }

    private void initView() {
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.getMallInfo(MallActivity.this, isFirst);
            }
        });
        final LinearLayoutManager layoutManager1 = new LinearLayoutManager(this);
        layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);//把布局设为横行排列
        mRvSaveDays.setLayoutManager(layoutManager1);
        mSaveDayAdapter = new SaveDayAdapter(new ArrayList<>());
        mRvSaveDays.setAdapter(mSaveDayAdapter);
        mRvSaveDays.setNestedScrollingEnabled(false);
        mSaveDayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MallBean.CateListBean bean = mSaveDayAdapter.getItem(position);
                Intent intent = new Intent(MallActivity.this, SortSearchActivity.class);
                intent.putExtra("id", String.valueOf(bean.getId()));
                intent.putExtra("name", bean.getName());
                intent.putExtra("type", 0 + "");
                startActivity(intent);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//把布局设为横行排列
        homeRecyclerView.setLayoutManager(layoutManager);
        mMallCenterAdapter = new MallCenterAdapter(new ArrayList<>());
        homeRecyclerView.setAdapter(mMallCenterAdapter);

        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);//把布局设为横行排列
        homeRecyclerView2.setLayoutManager(layoutManager2);
        mMallCenterAdapter2 = new MallCenterAdapter(new ArrayList<>());
        homeRecyclerView2.setAdapter(mMallCenterAdapter2);

        rollPagerView.setAnimationDurtion(500);
        rollPagerView.setHintView(new ColorPointHintView(this, getResources().getColor(R.color.c_90ff), getResources().getColor(R.color.white)));
        mBannerAdapter = new MallBannerAdapter(this, mBannerListBeans);
        rollPagerView.setAdapter(mBannerAdapter);

        mMallCenterAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final MallBean.KtProductListBean item = mMallCenterAdapter.getItem(position);
                Intent intent = new Intent(MallActivity.this, ShopDetailActivity.class);
                intent.putExtra("goodsId", String.valueOf(item.getId()));
                startActivity(intent);
            }
        });
        mMallCenterAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                final MallBean.KtProductListBean item = mMallCenterAdapter2.getItem(position);
                Intent intent = new Intent(MallActivity.this, ShopDetailActivity.class);
                intent.putExtra("goodsId", String.valueOf(item.getId()));
                startActivity(intent);
            }
        });
    }

    @OnClick({R.id.ll_search, R.id.ll_kt_shop, R.id.iv_kt_topic, R.id.ll_xf_shop, R.id.iv_xf_topic})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_search: {
                startActivity(new Intent(this, SearchActivity.class));
                break;
            }
            case R.id.iv_kt_topic:
            case R.id.ll_kt_shop: {
                Intent intent = new Intent(MallActivity.this, SortSearchActivity.class);
                intent.putExtra("id", "");
                intent.putExtra("name", "");
                intent.putExtra("type", 1 + "");
                startActivity(intent);
                break;
            }
            case R.id.iv_xf_topic:
            case R.id.ll_xf_shop: {
                Intent intent = new Intent(MallActivity.this, SortSearchActivity.class);
                intent.putExtra("id", "");
                intent.putExtra("name", "");
                intent.putExtra("type", 2 + "");
                startActivity(intent);
                break;
            }
        }
    }

    @Override
    public void getMallInfoSuccess(MallBean bean) {
        mSmartRefreshLayout.finishRefresh();
        final List<HomeBean.BannerListBean> bannerList = bean.getBannerList();
        mBannerListBeans.clear();
        if (bean.getCarNum() == 0) {
            mCarNumberTv.setVisibility(View.GONE);
        } else {
            mCarNumberTv.setText(String.valueOf(bean.getCarNum()));
            mCarNumberTv.setVisibility(View.VISIBLE);
        }
        if (bannerList != null && bannerList.size() > 0) {
            for (HomeBean.BannerListBean bannerListBean : bannerList) {
                mBannerListBeans.add(bannerListBean);
            }
        } else {
            rollPagerView.setVisibility(View.GONE);
            ivBannerPager.setVisibility(View.VISIBLE);
        }
        mBannerAdapter.notifyDataSetChanged();

        final List<MallBean.CateListBean> cateList = bean.getCateList();
        if (cateList != null && cateList.size() > 0) {
            mSaveDayAdapter.setNewData(cateList);
        }

        final List<MallBean.KtProductListBean> ktProductList = bean.getKtProductList();
        if (ktProductList != null && ktProductList.size() > 0) {
            mMallCenterAdapter.setNewData(ktProductList);
        }
        final List<MallBean.KtProductListBean> xfProductList = bean.getXfProductList();
        if (xfProductList != null && xfProductList.size() > 0) {
            mMallCenterAdapter2.setNewData(xfProductList);
        }
        final List<MallBean.ZtListBean> ztList = bean.getZtList();
        if (ztList != null && ztList.size() > 0) {
            final MallBean.ZtListBean ztListBean = ztList.get(0);
            setTopicContent(ztListBean);

            if (ztList.size() > 1) {
                final MallBean.ZtListBean ztListBean1 = ztList.get(1);
                setTopicContent(ztListBean1);
            }
        }
    }

    private void setTopicContent(MallBean.ZtListBean listBean) {
        if ("通证商品精选".equals(listBean.getTitle())) {
            tvKtTitle.setText(listBean.getTitle());
            if (TextUtils.isEmpty(listBean.getImgPath())) {
                GlideUtils.loadRoundImg(this, ivKtTopic, R.mipmap.banner_mall_center, 4);
            } else {
                GlideUtils.loadRoundImg(this, ivKtTopic, listBean.getImgPath(), R.mipmap.banner_mall_center, 4);
            }
        } else {
            tvXfTitle.setText(listBean.getTitle());
            if (TextUtils.isEmpty(listBean.getImgPath())) {
                GlideUtils.loadRoundImg(this, ivXfTopic, R.mipmap.banner_mall_center, 4);
            } else {
                GlideUtils.loadRoundImg(this, ivXfTopic, listBean.getImgPath(), R.mipmap.banner_mall_center, 4);
            }
        }
    }
}
